//
//  LaunchDelegate.m
//  cTypica
//
//  Created by David Kavanagh on 12/3/08.
//  Copyright 2008 directThought. All rights reserved.
//

#import "LaunchDelegate.h"


@implementation LaunchDelegate
@synthesize reservationReceiver, item, instance;
@synthesize inGroups, inInstances, inProductCodes;
@synthesize formatter;

+ (id)delegateWithReceiver:(id)receiver {
	LaunchDelegate *ret = [self alloc];
	ret.reservationReceiver = receiver;
	ret.formatter = [[NSDateFormatter alloc] init];
	[ret.formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
	return ret;
}

- (void) dealloc {
	[self.formatter release];
	[super dealloc];
}

// XML parser delegate methods
- (void)parser: (NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	//NSLog(@"<%@>", elementName);
	if ([elementName isEqualToString:@"RunInstancesResponse"]) {
		self.inGroups = NO;
		self.inInstances = NO;
		self.inProductCodes = NO;
		if ([reservationReceiver conformsToProtocol:@protocol(ReservationDescriptionProtocol)]) {
			[reservationReceiver startNewReservationDescription];
		}
		self.item = [ReservationDescription alloc];
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (self.inInstances && !self.inProductCodes) {
			self.instance = [Instance alloc];
		}
		else if (!self.inGroups && !self.inProductCodes) {
		}
	}
	else if ([elementName isEqualToString:@"groupSet"]) {
		[self.item setGroups:[[[NSMutableArray alloc] initWithCapacity:1] autorelease]];
		self.inGroups = YES;
	}
	else if ([elementName isEqualToString:@"instancesSet"]) {
		//NSLog(@"item = %@", [self item]);
		self.item.instances = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
		self.inInstances = YES;
	}
	else if ([elementName isEqualToString:@"productCodes"]) {
		[self.instance setProductCodes:[[[NSMutableArray alloc] initWithCapacity:1] autorelease]];
		self.inProductCodes = YES;
	}
}

- (void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSString *cleanString = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	[currentStringValue setString:@""];
	//NSLog(@"%@</%@>", cleanString, elementName);
	if ([elementName isEqualToString:@"RunInstancesResponse"]) {
		if ([reservationReceiver conformsToProtocol:@protocol(ReservationDescriptionProtocol)]) {
			if (![reservationReceiver reservationDescriptionReady:self.item]) {
				// stop all of this!
				// option 1: just stop sending this message, but process the rest of the XML
				// option 2: halt parsing alltogether
			}
			[self.item release];
			[reservationReceiver endNewReservationDescription];
		}
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (self.inInstances && !self.inProductCodes) {
			[self.item.instances addObject:instance];
			[self.instance release];
		}
		else if (!self.inInstances && !self.inGroups) {
		}
	}
	else if ([elementName isEqualToString:@"instanceId"]) {
		self.instance.instanceId = cleanString;
	}
	else if ([elementName isEqualToString:@"imageId"]) {
		self.instance.imageId = cleanString;
	}
	else if ([elementName isEqualToString:@"code"]) {
		self.instance.instanceStateCode = cleanString;
	}
	else if ([elementName isEqualToString:@"name"]) {
		self.instance.instanceStateName = cleanString;
	}
	else if ([elementName isEqualToString:@"privateDnsName"]) {
		self.instance.privateDnsName = cleanString;
	}
	else if ([elementName isEqualToString:@"dnsName"]) {
		self.instance.dnsName = cleanString;
	}
	else if ([elementName isEqualToString:@"reason"]) {
		self.instance.reason = cleanString;
	}
	else if ([elementName isEqualToString:@"keyName"]) {
		self.instance.keyName = cleanString;
	}
	else if ([elementName isEqualToString:@"amiLaunchIndex"]) {
		self.instance.launchIndex = cleanString;
	}
	else if ([elementName isEqualToString:@"instanceType"]) {
		self.instance.instanceType = cleanString;
	}
	else if ([elementName isEqualToString:@"launchTime"]) {
		self.instance.launchTime = [formatter dateFromString:cleanString];
		//[cleanString release];
	}
	else if ([elementName isEqualToString:@"availabilityZone"]) {
		self.instance.availabilityZone = cleanString;
	}
	else if ([elementName isEqualToString:@"kernelId"]) {
		self.instance.kernelId = cleanString;
	}
	else if ([elementName isEqualToString:@"ramdiskId"]) {
		self.instance.ramdiskId = cleanString;
	}
	else if ([elementName isEqualToString:@"groupId"]) {
		[self.item.groups addObject:cleanString];
	}
	else if ([elementName isEqualToString:@"groupSet"]) {
		self.inGroups = NO;
	}
	else if ([elementName isEqualToString:@"instancesSet"]) {
		self.inInstances = NO;
	}
	else if ([elementName isEqualToString:@"productCode"]) {
		[self.instance.productCodes addObject:currentStringValue];
	}
	else if ([elementName isEqualToString:@"productCodes"]) {
		self.inProductCodes = NO;
	}
	else if ([elementName isEqualToString:@"reservationId"]) {
		self.item.resId = currentStringValue;
	}
	else if ([elementName isEqualToString:@"ownerId"]) {
		self.item.owner = currentStringValue;
	}
	//[cleanString release];
}
@end
